home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 9 / Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO / 009a / snpd0493.zip / TRAPFLAG.ASM < prev    next >
Assembly Source File  |  1993-04-05  |  4KB  |  134 lines

  1.         PAGE ,132
  2.  
  3. ;  Install a custom Interrupt 23 (Ctrl-C exception) handler
  4. ;
  5. ;  Public domain by Bob Stout
  6. ;
  7. ;  Requires MASM 5.1 or later or equivalent
  8. ;
  9. ;  Assemble with:       MASM /Mx /z ...
  10. ;                       TASM /jMASM /mx /z ...
  11.  
  12. %       .MODEL  memodel,C               ;Add model support via command
  13.                                         ;line macros, e.g.
  14.                                         ;MASM /Dmemodel=LARGE
  15.  
  16. kbstatseg       equ     40h
  17. kbstatofs       equ     17h
  18. ctl_on          equ     0100b
  19. alt_on          equ     1000b
  20. sk_c            equ     2eh
  21. EOI             equ     20h
  22. PIC             equ     20h
  23. kb_inp          equ     60h
  24. kb_outp         equ     61h
  25.  
  26.         .CODE
  27.  
  28. _oldvec09       dd      ?
  29. _oldvec1b       dd      ?
  30.  
  31.         public  ccrcvd
  32.  
  33. ccrcvd          dw      0
  34.  
  35. ;
  36. ;  This is our actual ISR
  37. ;
  38. myint09:
  39.         push    ax                      ;save AX...
  40.         pushf                           ;  ...& flags
  41.         in      al,kb_inp               ;get scan code
  42.         cmp     al,sk_c                 ;'C'?
  43.         jne     do_old                  ;no, forget it
  44.  
  45.         push    ax                      ;yes, save it...
  46.         push    es                      ;...& ES reg
  47.         mov     ax,kbstatseg            ;read keyboard status from 40:17
  48.         mov     es,ax
  49.         mov     al,es:kbstatofs
  50.         test    al,ctl_on               ;Ctrl pressed?
  51.         pop     es                      ;(restore AX, ES)
  52.         pop     ax
  53.         jz      do_old                  ;no, forget it
  54.  
  55.         in      al,kb_outp              ;yes, toggle keyboard acknowledge line
  56.         mov     ah,al
  57.         or      al,80h
  58.         out     kb_outp,al
  59.         xchg    al,ah
  60.         out     kb_outp,al
  61.         cli
  62.         mov     ax,EOI                  ;send end-of-interrupt code
  63.         out     PIC,al
  64.         mov     ax,1
  65.         mov     CS:ccrcvd,ax
  66.         pop     ax                      ;discard original flags...
  67.         pop     ax                      ; ...& AX
  68.         iret                            ;all done
  69. do_old:
  70.         popf                            ;restore flags...
  71.         pop     ax                      ; ...& AX
  72.         jmp     dword PTR CS:_oldvec09  ;call our handler
  73.  
  74. ;
  75. ;  To avoid keyboard confusion, trap Ctrl-Break separately
  76. ;
  77. myint1b:
  78.         push    ax
  79.         pushf
  80.         mov     ax,-1
  81.         mov     CS:ccrcvd,ax
  82.         popf
  83.         pop     ax
  84.         iret
  85.  
  86. ;
  87. ;  Call this to uninstall our ISR
  88. ;
  89. undo09  PROC    USES DX DS AX
  90.         mov     dx, word PTR CS:_oldvec09 ;restore original keyboard vector
  91.         mov     ds, word PTR CS:_oldvec09+2
  92.         mov     ax,2509h
  93.         int     21h
  94.  
  95.         mov     dx, word PTR CS:_oldvec1b ;restore original keyboard vector
  96.         mov     ds, word PTR CS:_oldvec1b+2
  97.         mov     ax,251bh
  98.         int     21h
  99.  
  100.         ret
  101. undo09  ENDP
  102.  
  103. ;
  104. ;  Call this to install  our ISR
  105. ;
  106. ins09   PROC    USES AX BX DS ES
  107.  
  108.         mov     ax,3509h                ;get old keyboard ISR vector...
  109.         int     21h
  110.         mov     word PTR CS:_oldvec09,bx
  111.         mov     word PTR CS:_oldvec09+2,es ;...and save it
  112.  
  113.         mov     ax,351bh                ;get old exit vector...
  114.         int     21h
  115.         mov     word PTR CS:_oldvec1b,bx
  116.         mov     word PTR CS:_oldvec1b+2,es ;...and save it
  117.  
  118.         push    cs                      ;get myint09 segment in DS
  119.         pop     ds
  120.         mov     dx, OFFSET myint09      ;install myint09 in int 09h
  121.         mov     ax,2509h
  122.         int     21h
  123.  
  124.         push    cs                      ;get myint1b segment in DS
  125.         pop     ds
  126.         mov     dx, OFFSET myint1b      ;install myint1b in int 1bh
  127.         mov     ax,251bh
  128.         int     21h
  129.  
  130.         ret
  131. ins09   ENDP
  132.  
  133.         end
  134.